﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Objects;

namespace medder
{
    public partial class PatientBook : Form
    {
        MedBaseEntities med = new MedBaseEntities();
        public static ObjectSet<User> users;
        public static ObjectSet<Patient> patients;
        public static ObjectSet<Insurance> insurances;
        public static ObjectSet<Region> regions;

        public PatientBook()
        {
            InitializeComponent();
        }

        private void PatientBook_Load(object sender, EventArgs e)
        {
            users = med.Users;
            patients = med.Patients;
            insurances = med.Insurances;
            regions = med.Regions;

            GridHelper.CreateColumnsCommon(bookView);

            bookView.Columns.Add("BirthDay", "Дата рождения");
            bookView.Columns["BirthDay"].ReadOnly = true;

            bookView.Columns.Add("Address", "Адрес");
            bookView.Columns["Address"].ReadOnly = true;

            bookView.Columns.Add("Sex", "Пол");
            bookView.Columns["Sex"].ReadOnly = true;
            bookView.Columns["Sex"].Width = 30;

            bookView.Columns.Add("Unsurance", "Полюс");
            bookView.Columns["Unsurance"].ReadOnly = true;

            DataGridViewComboBoxColumn region = new DataGridViewComboBoxColumn();
            foreach (Region x in regions)
            {
                region.Items.Add(x.Name);
            }
            region.Name = "Region";
            region.HeaderText = "Участок";
            region.Width = 100;
            bookView.Columns.Add(region);

            

            string sex = string.Empty;
            int i = 0;
            foreach (Patient pat in patients)
            {
                bookView.Rows.Add();
                User user = users.Where(x => x.Passport == pat.Passport).FirstOrDefault();
                bookView.Rows[i].Cells[0].Value = GridHelper.PresentPassport(user.Passport);
                bookView.Rows[i].Cells[1].Value = user.Name;
                bookView.Rows[i].Cells[2].Value = user.Surname;
                bookView.Rows[i].Cells[3].Value = user.Patronymic;

                string birthday = pat.Birthday == null ? string.Empty : pat.Birthday.Value.ToShortDateString();
                bookView.Rows[i].Cells[4].Value = birthday;
                bookView.Rows[i].Cells[5].Value = pat.Address;
                
                sex = pat.Sex==null ? string.Empty  : ((bool)pat.Sex ? "муж" : "жен");
                bookView.Rows[i].Cells[6].Value = sex;

                Insurance ins = insurances.Where(x => x.InsId == pat.InsId).FirstOrDefault();
                bookView.Rows[i].Cells[7].Value = ins.InsName;

                Region regio = regions.Where(x=>x.RegId==pat.Region).FirstOrDefault();
                string regionName = regio == null ? string.Empty : regio.Name;
                bookView.Rows[i].Cells[8].Value = regionName;

                i++;
            }
            
        }

        private void добавитьToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void пользователяToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (bookView.CurrentRow != null)
            {
                string passport = GridHelper.SavePassport(bookView.CurrentRow.Cells[0].Value.ToString());

                EditUser eu = new EditUser(passport);
                eu.ShowDialog();
            }
            else
                MessageBox.Show("Выберите строку для удаления");

           
        }
    }
}
